package com.java.example.util

import android.content.Context
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import com.alibaba.fastjson2.JSON
import com.blankj.utilcode.util.SPUtils
import com.java.example.app.Constant
import org.json.JSONArray
import org.json.JSONObject
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

object AccountUtil {
    private const val SP_NAME = "ACCOUNT"

    /**
     * 使用SharedPreference保存序列化对象
     * 用Base64.encode将字节文件转换成Base64编码保存在String中
     *
     * @param context 上下文
     * @param key     储存对象的key
     * @param object  object对象  对象必须实现Serializable序列化，否则会出问题，
     * out.writeObject 无法写入 Parcelable 序列化的对象
     */
    fun putObject(context: Context?, key: String?, `object`: Any?) {
        if (context == null) {
            return
        }
        val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        //创建字节输出流
        val baos = ByteArrayOutputStream()
        //创建字节对象输出流
        var out: ObjectOutputStream? = null
        try {
            //然后通过将字对象进行64转码，写入sp中
            out = ObjectOutputStream(baos)
            out.writeObject(`object`)
            val objectValue = String(Base64.encode(baos.toByteArray(), Base64.DEFAULT))
            val editor = sp.edit()
            editor.putString(key, objectValue)
            editor.commit()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                if (baos != null) {
                    baos.close()
                }

                out?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 获取SharedPreference保存的对象
     * 使用Base64解密String，返回Object对象
     *
     * @param context 上下文
     * @param key     储存对象的key
     * @param <T>     泛型
     * @return 返回保存的对象
    </T> */
    fun <T> getObject(context: Context, key: String?): T? {
        val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        if (sp.contains(key)) {
            val objectValue = sp.getString(key, null)
            val buffer = Base64.decode(objectValue, Base64.DEFAULT)
            //一样通过读取字节流，创建字节流输入流，写入对象并作强制转换
            val bais = ByteArrayInputStream(buffer)
            var ois: ObjectInputStream? = null
            try {
                ois = ObjectInputStream(bais)
                val t = ois.readObject() as T
                return t
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                try {
                    if (bais != null) {
                        bais.close()
                    }

                    ois?.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return null
    }

    @JvmStatic
    fun clearAccount(context: Context, key: String?) {
        val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        if (sp.contains(key)) {
            sp.edit().clear().commit()
        }
    }



    /**
     * 判断手机中是否安装指定包名的软件
     */
    fun isInstallApk(context: Context, name: String): Boolean {
        val packages = context.packageManager.getInstalledPackages(0)
        for (i in packages.indices) {
            val packageInfo = packages[i]
            if (packageInfo.packageName == name) {
                return true
            } else {
                continue
            }
        }
        return false
    }

}
